package com.quectel.core.module.entranceguard.service.impl;


import com.quectel.constant.global.SystemConstants;
import com.quectel.core.constants.RedisCacheConstants;
import com.quectel.core.module.device.dto.DeviceDto;
import com.quectel.core.module.device.service.DeviceService;
import com.quectel.core.module.entranceguard.dao.EntranceGuardPersonCardDao;
import com.quectel.core.module.entranceguard.dto.EntranceGuardPersonCardDto;
import com.quectel.core.module.entranceguard.dto.VisitorDto;
import com.quectel.core.module.entranceguard.entity.EntranceGuardPersonCardEntity;
import com.quectel.core.module.entranceguard.service.EntranceGuardPersonCardService;
import com.quectel.core.module.entranceguard.service.VisitorService;
import com.quectel.core.module.household.dto.HouseholdDto;
import com.quectel.core.module.household.service.HouseholdService;
import com.quectel.util.common.CopyUtils;
import com.quectel.util.kit.CacheKit;
import com.quectel.util.kit.Snowflake;
import com.quectel.util.mybatis.MyBatisPlusUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @author caolu
 * @email louis.cao@quectel.com
 * @date 2021-11-18 16:28:38
 */
@DubboService
public class EntranceGuardPersonCardServiceImpl implements EntranceGuardPersonCardService {

    private static final Logger LOGGER = LoggerFactory.getLogger(EntranceGuardPersonCardServiceImpl.class);

    private static final String ENTRANCE_GUARD_PERSON_CARD_CACHE_BY_ID = RedisCacheConstants.PROJECT_NAME + "ENTRANCE_GUARD_PERSON_CARD_CACHE_BY_ID:";

    @Autowired
    private EntranceGuardPersonCardDao entranceGuardPersonCardDao;

    @Autowired
    private DeviceService deviceService;
    @Autowired
    private HouseholdService householdService;
    @Autowired
    private VisitorService visitorService;


    @Override
    public EntranceGuardPersonCardDto selectCacheById(Long id) {
        return CacheKit.cacheToRedis(
                () -> selectById(id),
                ENTRANCE_GUARD_PERSON_CARD_CACHE_BY_ID + id,

                SystemConstants.NOT_NULL_CACHE_EXPIRE_SECONDS
        );
    }

    @Override
    public EntranceGuardPersonCardDto selectById(Long id) {
        EntranceGuardPersonCardEntity entity = entranceGuardPersonCardDao.selectById(id);
        return paddingField(CopyUtils.copyObj(entity, EntranceGuardPersonCardDto.class));
    }

    @Override
    public List<EntranceGuardPersonCardDto> queryList(Map<String, Object> params) {
        List<EntranceGuardPersonCardEntity> list = entranceGuardPersonCardDao.queryList(params);
        List<EntranceGuardPersonCardDto> result = CopyUtils.copyList(list, EntranceGuardPersonCardDto.class);
        result.forEach(this::paddingField);
        return result;
    }

    private EntranceGuardPersonCardDto paddingField(EntranceGuardPersonCardDto dto) {
        if (dto != null) {
            DeviceDto deviceDto = deviceService.selectCacheById(dto.getDeviceId());
            if (deviceDto != null) {
                dto.setDeviceImei(deviceDto.getImei());
                dto.setDeviceName(deviceDto.getName());
            }
            if (SystemConstants.PlatformPersonType.HOUSEHOLD.getType().equals(dto.getPersonType())) {
                HouseholdDto householdDto = householdService.selectCacheById(dto.getPersonId());
                if (householdDto != null) {
                    dto.setPersonName(householdDto.getName());
                }
            } else if (SystemConstants.PlatformPersonType.VISITOR.getType().equals(dto.getPersonType())) {
                VisitorDto visitorDto = visitorService.selectCacheById(dto.getPersonId());
                if (visitorDto != null) {
                    dto.setPersonName(visitorDto.getName());
                }
            }
        }
        return dto;
    }

    @Override
    public int queryTotal(Map<String, Object> params) {
        return entranceGuardPersonCardDao.queryTotal(params);
    }

    @Override
    public Long save(EntranceGuardPersonCardDto dto) {
        EntranceGuardPersonCardEntity entity = CopyUtils.copyObj(dto, EntranceGuardPersonCardEntity.class);
        entity.setId(Snowflake.nextId());
        entranceGuardPersonCardDao.insert(entity);
        return entity.getId();
    }

    @Override
    public void updateById(EntranceGuardPersonCardDto dto) {
        EntranceGuardPersonCardEntity entity = CopyUtils.copyObj(dto, EntranceGuardPersonCardEntity.class);
        entranceGuardPersonCardDao.updateById(entity);
        CacheKit.invalidRedisCache(ENTRANCE_GUARD_PERSON_CARD_CACHE_BY_ID + dto.getId());
    }

    @Override
    public void updateAllColumnById(EntranceGuardPersonCardDto dto) {
        EntranceGuardPersonCardEntity entity = CopyUtils.copyObj(dto, EntranceGuardPersonCardEntity.class);


        MyBatisPlusUtils.updateAllColumnById(entity, entranceGuardPersonCardDao);

        CacheKit.invalidRedisCache(ENTRANCE_GUARD_PERSON_CARD_CACHE_BY_ID + dto.getId());
    }

    @Override
    public void deleteById(Long id) {
        entranceGuardPersonCardDao.deleteById(id);

        CacheKit.invalidRedisCache(ENTRANCE_GUARD_PERSON_CARD_CACHE_BY_ID + id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] ids) {
        for (Long id : ids) {
            deleteById(id);
        }
    }
}
